package com.jwthhealth_pub.old.diet.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.diet.view.DietSecActivity;
import com.jwthhealth_pub.old.diet.view.fragment.adatper.DietNursedAdatper;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/27.
 */
public class NursedFragment extends Fragment {

    public static final int JUMP = 0;
    public static final int TOAS = 1;

    @BindView(R.id.rb)
    RecyclerView rv;
    @BindView(R.id.progressbar)
    ProgressBar progressBar;

    private DietNursedAdatper adapter;

    public Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case JUMP:
                    Bundle data = msg.getData();
                    Intent intent = new Intent(getActivity(), DietSecActivity.class);
                    intent.putExtras(data);
                    startActivity(intent);
                    progressbar();
                    adapter.setLoading(false);
                    break;
                case TOAS:
                    Toast.makeText(getActivity(), msg.getData().getString(Constant.DIETDATA), Toast.LENGTH_SHORT).show();
                    progressbar();
                    adapter.setLoading(false);
                    break;
                default:break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = null;

        view = inflater.inflate(R.layout.fragment_nursed, container, false);

        ButterKnife.bind(this, view);

        initRecyclerView();
        return view;
    }

    private void initRecyclerView() {
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new DietNursedAdatper(getActivity());
        adapter.setHandler(mHandler);
        adapter.setProgressBar(progressBar);
        rv.setAdapter(adapter);
    }

    private void progressbar(){
        if (progressBar.getVisibility() == View.VISIBLE) {
            progressBar.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}
